package com.example.dell.fastshopping.user;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.user.entity.IntegralBaseInformation;
import com.example.dell.fastshopping.user.entity.IntegralDetail;
import com.example.dell.fastshopping.user.modle.IntegralActivityMolder;
import com.example.dell.fastshopping.utils.DateUtils;
import com.example.dell.fastshopping.views.BaseListActivity;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class IntegralActivity extends BaseListActivity {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.current_jifen)
    TextView currentJifen;
    @BindView(R.id.price)
    TextView price;
    @BindView(R.id.start)
    ImageView start;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.ci)
    TextView ci;
    @BindView(R.id.back_integral)
    ImageView backIntegral;
    @BindView(R.id.nodata)
    LinearLayout nodata;
    @BindView(R.id.refresh)
    TwinklingRefreshLayout refresh;
    private IntegralActivityMolder molder;
    private List<IntegralDetail> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral);
        ButterKnife.bind(this);
        super.initData();
        initView();
    }


    @Override
    protected TwinklingRefreshLayout getRefreshLayout() {
        return refresh;
    }

    private void initView() {
        title.setText("积分");
        molder = ViewModelProviders.of(this).get(IntegralActivityMolder.class);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        molder.getUserIntegral().observe(this, new Observer<IntegralBaseInformation>() {
            @Override
            public void onChanged(@Nullable IntegralBaseInformation integralBaseInformation) {
                currentJifen.setText(integralBaseInformation.getIntegral() + "");
                price.setText(integralBaseInformation.getPriceString());
                int status = integralBaseInformation.getStatus();
                if (status == -1) {
                    start.setImageResource(R.mipmap.downdowndown);
                } else if (status == 0) {

                } else {
                    start.setImageResource(R.mipmap.upupup);
                }
            }
        });

        getNetData(1, 15);
    }

    @Override
    protected void getNetData(final int page, int pageSize) {
        molder.getList(page, pageSize).observe(this, new Observer<List<IntegralDetail>>() {
            @Override
            public void onChanged(@Nullable List<IntegralDetail> InteList) {
                if (page == 1) {
                    list = InteList;
                } else {
                    list.addAll(InteList);
                }
                if (list.size() == 0) {
                    nodata.setVisibility(View.VISIBLE);
                } else {
                    nodata.setVisibility(View.GONE);
                }
                RecyclerView.Adapter adapter = rv.getAdapter();
                if (adapter == null) {
                    rv.setAdapter(new MyAdapter());
                } else {
                    adapter.notifyDataSetChanged();
                }
                loadFinsh();
            }
        });
    }

    @OnClick(R.id.back)
    public void onClick() {
        finish();
    }


    private class MyAdapter extends RecyclerView.Adapter<MyHoldle> {
        @NonNull
        @Override
        public MyHoldle onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View inflate = LayoutInflater.from(IntegralActivity.this).inflate(R.layout.item_integral, viewGroup, false);
            MyHoldle myHoldle = new MyHoldle(inflate);
            return myHoldle;
        }

        @Override
        public void onBindViewHolder(@NonNull MyHoldle myHoldle, int i) {
            IntegralDetail integralDetail = list.get(i);
            myHoldle.jifenType.setText(integralDetail.getTypeString());
            myHoldle.time.setText(DateUtils
                    .stampToDate(integralDetail.getCreateTime()));
            myHoldle.num.setText(integralDetail.getCostString());
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    private class MyHoldle extends RecyclerView.ViewHolder {

        public TextView jifenType;
        public TextView time;
        public TextView num;

        public MyHoldle(View inflate) {
            super(inflate);
            jifenType = inflate.findViewById(R.id.jifen_type);
            time = inflate.findViewById(R.id.time);
            num = inflate.findViewById(R.id.num);
        }
    }
}
